package com.ghd.dataviz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghd.dataviz.constant.FileType;
import com.ghd.dataviz.constant.UploadPath;
import com.ghd.dataviz.core.param.PageInfo;
import com.ghd.dataviz.core.utils.UnZipAnRarUtil;
import com.ghd.dataviz.entity.FileUploadInfo;
import com.ghd.dataviz.entity.SyncPageInfo;
import com.ghd.dataviz.entity.User;
import com.ghd.dataviz.mapper.SyncPageInfoMapper;
import com.ghd.dataviz.param.SyncPageParam;
import com.ghd.dataviz.service.IFileUploadInfoService;
import com.ghd.dataviz.service.ISyncPageInfoService;
import com.ghd.dataviz.utils.PageUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 大屏动态页面实现类
 * </p>
 *
 * @author guohd
 * @since 2020-03-23
 */
@Service
public class SyncPageInfoServiceImpl extends ServiceImpl<SyncPageInfoMapper, SyncPageInfo> implements ISyncPageInfoService {

    @Autowired
    private UploadPath uploadPath;

    private final IFileUploadInfoService fileUploadInfoService;

    public SyncPageInfoServiceImpl(IFileUploadInfoService fileUploadInfoService) {
        this.fileUploadInfoService = fileUploadInfoService;
    }

    @SneakyThrows
    @Override
    public SyncPageInfo insertPageInfo(SyncPageInfo syncPageInfo) {
        FileUploadInfo fileUploadInfo = fileUploadInfoService.getById(syncPageInfo.getFileId());
        String fileType = fileUploadInfo.getType();
        if (FileType.ZIP.getType().equals(fileType)) {
            long nanoTime = System.nanoTime();
            File file = new File(fileUploadInfo.getPath());
            String outPath = uploadPath.getPlugin() + File.separator + nanoTime;
            UnZipAnRarUtil.unZip(file, outPath);
            String fileName = syncPageInfo.getCode() + ".umd.min.js";
            syncPageInfo.setPath(outPath + File.separator + fileName);
            syncPageInfo.setUrl(nanoTime + "/" + fileName);
            this.save(syncPageInfo);
            return syncPageInfo;
        }
        return null;
    }

    @Override
    public List<SyncPageInfo> getList() {
        return list();
    }

    @Override
    public void editPage(SyncPageInfo syncPageInfo) {
        this.updateById(syncPageInfo);
    }

    @Override
    public PageInfo<SyncPageInfo> findSyncPageList(SyncPageParam pageParam) {
        Page<SyncPageInfo> page = PageUtil.convertParam(pageParam);
        String name = "%" + pageParam.getName() + "%";
        String code = "%" + pageParam.getCode() + "%";
        Page<SyncPageInfo> syncPageInfos = this.baseMapper.findSyncPageList(page, name, code);
        return PageUtil.convertPage(syncPageInfos);
    }

    @Override
    public List<SyncPageInfo> findDisplayPage() {
        QueryWrapper<SyncPageInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        return list(queryWrapper);
    }
}
